package com.shaunstone.web.sports.dal.model;

import java.io.Serializable;

import javax.persistence.*;

import org.hibernate.annotations.Cascade;
import org.springframework.data.repository.cdi.Eager;

import java.util.List;

/**
 * The persistent class for the team database table.
 * 
 */
@Entity
@Table(name = "team")
@NamedQuery(name = "Team.findAll", query = "SELECT t FROM Team t")
public class Team implements Serializable {
	private static final long serialVersionUID = 1L;

	public Team(String gender, String name, List<PersonTeamMapping> personTeamMappings, List<Division> divisions) {
		super();
		// this.id = id;
		this.gender = gender;
		this.name = name;
		this.personTeamMappings = personTeamMappings;
		this.divisions = divisions;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	private String gender;

	private String name;

	// bi-directional many-to-one association to PersonTeamMapping

	@OneToMany(mappedBy = "team", fetch = FetchType.EAGER, orphanRemoval = true)
	private List<PersonTeamMapping> personTeamMappings;

	// bi-directional many-to-many association to Division
	@ManyToMany(mappedBy = "teams")
	private List<Division> divisions;

	public Team() {
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getGender() {
		return this.gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<PersonTeamMapping> getPersonTeamMappings() {
		return this.personTeamMappings;
	}

	public void setPersonTeamMappings(List<PersonTeamMapping> personTeamMappings) {
		this.personTeamMappings = personTeamMappings;
	}

	public PersonTeamMapping addPersonTeamMapping(PersonTeamMapping personTeamMapping) {
		getPersonTeamMappings().add(personTeamMapping);
		personTeamMapping.setTeam(this);

		return personTeamMapping;
	}

	public PersonTeamMapping removePersonTeamMapping(PersonTeamMapping personTeamMapping) {
		getPersonTeamMappings().remove(personTeamMapping);
		personTeamMapping.setTeam(null);

		return personTeamMapping;
	}

	public List<Division> getDivisions() {
		return this.divisions;
	}

	public void setDivisions(List<Division> divisions) {
		this.divisions = divisions;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (gender == null) {
			if (other.gender != null)
				return false;
		} else if (!gender.equals(other.gender))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}